var util=require('../../../util/util.js');
var app=getApp();
Page({
  data: {
    movies:[]
  },
  onLoad: function (options) {
  var title=options.type;
  this.setData({
    title:title
  })
  var url='';
  switch(title){
    case '正在热映':
    url=app.globalData.doubanBase+"/v2/movie/in_theaters";
    break;
    case '即将上映':
    url=app.globalData.doubanBase+"/v2/movie/coming_soon";
    break;
    case '豆瓣Top250':
    url=app.globalData.doubanBase+"/v2/movie/top250";
    break;
  }
  this.data.url=url;
  util.http(url,this.deal)
  },
  onReady: function () {
    wx.setNavigationBarTitle({
     title:this.data.title
    })
    wx.showNavigationBarLoading()
  },
  deal:function(data){
    var movies=[];
    for(var i in data.subjects){
      var subject=data.subjects[i];
      var temp={
        stars:util.convertToStarsArray(subject.rating.stars),
        title:subject.title,
        average:subject.rating.average,
        coverageUrl:subject.images.large,
        movieId:subject.id
      }
      movies.push(temp)
    }
    var totalMovies=[];
    totalMovies=this.data.movies.concat(movies);
    this.setData({
      movies:totalMovies
    })
    wx.stopPullDownRefresh()
    wx.hideNavigationBarLoading();
  },
  onpullDownRefresh:function(){
    var url=this.data.url+'?start=0&count=20';
    this.data.movies=[];
    unil.http(url,this.deal);
    wx.showNavigationBarLoading()
  },
  onReachBottom:function(event){
    var totalCount=this.data.movies.length;
    var nextUrl=this.data.url+"?count=20&start="+totalCount;
    util.http(nextUrl,this.deal)
    wx.showNavigationBarLoading()
  },
  todetail:function(event){
    var id=event.currentTarget.dataset.id;
    wx.navigateTo({
      url:'../movie-detail/movie-detail?id='+id
    })
  }
})